package cn.caixiong.org.controller;


import cn.caixiong.basic.util.AjaxResult;
import cn.caixiong.basic.util.PageList;
import cn.caixiong.org.domain.Employee;
import cn.caixiong.org.query.DepartmentQuery;
import cn.caixiong.org.query.EmployeeQuery;
import cn.caixiong.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController//交给Spring管理
@RequestMapping("/emp")
@Api(tags = "员工管理接口", description = "员工管理接口描述")
public class EmployeeController {
    //注入业务层
    @Autowired
    private IEmployeeService employeeService;

    //分页查询
    @PostMapping("/findPage")
    @ApiOperation(value = "分页查询所有员工接口", notes = "分页查询员工列表")
    public PageList<Employee> findPage(@RequestBody EmployeeQuery query) {
        //PageList<Employee> page = employeeService.findPage(query);
        return employeeService.findPage(query);
    }

    //查询所有
    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有员工接口", notes = "上级员工列表")
    public AjaxResult findAll() {
        return AjaxResult.createSuccess(employeeService.findAll());
    }

    //查询所有经理
    @GetMapping("/findManager")
    @ApiOperation(value = "查询所有经理接口", notes = "经理列表")
    public AjaxResult findManager() {
        return AjaxResult.createSuccess(employeeService.findManager());
    }

    //员工查询
    @GetMapping("/departmentMapper")
    @ApiOperation(value = "查询上级员工接口", notes = "员工列表")
    public AjaxResult departmentMapper() {
        return AjaxResult.createSuccess(employeeService);
    }

    //新增或修改
    @PostMapping
    @ApiOperation(value = "新增修改员工接口", notes = "没有id就是新增")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "department", value = "员工对象", required = true)})
    public AjaxResult saveOrUpdate(@RequestBody Employee employee) {
        try {
            employeeService.saveOrUpdate(employee);
            return AjaxResult.createSuccess(null);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError("操作异常，请联系管理员");
        }

    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "删除员工接口", notes = "根据删除id对象")
    public AjaxResult delete(@PathVariable("id") Long Id) {
        try {
            employeeService.delete(Id);
            return AjaxResult.createSuccess(null);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError("操作异常，请联系管理员");
        }

    }

    @PatchMapping
    @ApiOperation(value = "批量删除员工接口", notes = "批量删除id对象")
    public AjaxResult batchDelete(@RequestParam("ids") Long[] Ids) {
        try {
            employeeService.batchDelete(Ids);
            return AjaxResult.createSuccess(null);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError("操作异常，请联系管理员");
        }

    }


}
